package com.unitd.logistics.controller;

import com.unitd.logistics.comm.enums.ResultEnum;
import com.unitd.logistics.comm.exception.LogisticsException;
import com.unitd.logistics.comm.plugins.module.controller.BaseController;
import com.unitd.logistics.comm.spring.result.AjaxResult;
import com.unitd.logistics.service.ExportTableDataToFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件上传、下载前端控制器
 *
 * @author hudan
 * @version 1.0
 * @filename FileDealWithController.java
 * @copyright www.unitd.com
 * @date 2020-05-22
 */
@RestController
@RequestMapping("/file")
@Slf4j
public class FileDealWithController extends BaseController {

	@Autowired
	private ExportTableDataToFileService exportTableDataToFileService;

	/**
	 * 实现将指定数据库的表结构数据导出到指定文件上
	 *
	 * @param file 需要进行保存的excel文件路径
	 * @return 响应结果
	 */
	@GetMapping(value = "/export")
	public AjaxResult exportTableDataToExcel(@RequestParam("file") String file) {

		try {
			exportTableDataToFileService.exportTableDataToExcel(file);
			log.info("导入文件完成!");
			return new AjaxResult();
		} catch (Exception e) {
			throw new LogisticsException(ResultEnum.UPLOAD_FILE_ERROR);
		}
	}
}